package controller;

import javafx.fxml.FXML;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import tools.JDBCUtils;
import view.RegisterFrame;

import javax.swing.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author :wn
 * @program : 管家婆项目
 * @descript : 注册界面控制器类
 * @create :2020-06-24 22:20
 */

public class Register {
    @FXML
    private TextField username;
    @FXML
    private PasswordField userpassword;
    @FXML
    private PasswordField userpassword2;
    public void registerAction() throws SQLException, ClassNotFoundException {
        if (!username.getText().equals("")) {
            if(userpassword.getText().equals(userpassword2.getText())) {
                Statement smt = null;
                String url = "jdbc:mysql://localhost:3306/users?useSSL=false";
                Connection con = JDBCUtils.getConnection(url);
                String sql = "insert into familyuser(username ,userpassword ) values('" + username.getText() + "','" + userpassword2.getText() + "')";
                smt = con.createStatement();
                smt.execute(sql);
                JOptionPane.showMessageDialog(null,"注册成功");
            }else{
                JOptionPane.showMessageDialog(null,"两次密码不一致");
            }
        }else{
            JOptionPane.showMessageDialog(null,"请输入用户名");
        }
    }
    public void cancelAction() throws IOException {
       JOptionPane.showMessageDialog(null,"确定退出");
        RegisterFrame.stage.close();
    }
}
